/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;
import Data.UseCaseModel;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import gui.*;
import java.awt.*;
import javax.swing.*;
import java.util.Vector;
import java.io.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.Document;
/**
 *
 * @author Sean
 */
public class DiagramController {

    private Vector ucVector;

    public void DiagramController(){
        this.ucVector = UseCaseModel.getUseCaseVector();
        return;
    }
    public static void generatePressed(JTabbedPane jTabbedPane1, DrawPanel dPane, UseCaseController evntController){
       jTabbedPane1.add("Diagram", dPane);
       jTabbedPane1.setSelectedIndex(1);
       //String msg = "";
       /*for(int i = 0;i < evntController.myUCModel.getUseCaseVector().size(); i++){
           msg = msg + "\n" + evntController.myUCModel.getUseCaseVector().elementAt(i).toString();
       }
       JOptionPane.showMessageDialog(dPane, msg);*/
    }
    public void closePressed(JTabbedPane jTabbedPane1){
       jTabbedPane1.remove(1);
    }
    public void saveAsPressed(DrawPanel dPane) throws FileNotFoundException{
        JFrame frame = new JFrame();
        JFileChooser chooser = new JFileChooser();
        int returnVal = chooser.showSaveDialog(frame);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File newFile = chooser.getSelectedFile();
            FileOutputStream out = new FileOutputStream(newFile);
            saveComponentAsJPEG(dPane, newFile.getName(), out);
        }
    }

    public void saveComponentAsJPEG(Component myComponent, String filename, FileOutputStream out) {
       Dimension size = myComponent.getSize();
       BufferedImage myImage =
         new BufferedImage(size.width-250, size.height, BufferedImage.TYPE_INT_RGB);
       Graphics2D g2 = myImage.createGraphics();

       myComponent.paintAll(g2);
       try {
         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
         encoder.encode(myImage);
         out.close();
       } catch (Exception e) {
         System.out.println(e);
       }
     }

    public void savePressed(DrawPanel dPane, UseCaseGUI gui, JTable useCaseTable){
        JFrame frame = new JFrame();
        JFileChooser chooser = new JFileChooser();
        int returnVal = chooser.showSaveDialog(frame);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File newFile = chooser.getSelectedFile();
            try{
                FileOutputStream f_out = new FileOutputStream(newFile);
                // Write object with ObjectOutputStream
                ObjectOutputStream obj_out = new ObjectOutputStream (f_out);
                // Write object out to disk

                obj_out.writeObject ( UseCaseController.myUCModel.getUseCaseVector() );
                obj_out.writeObject ( gui.getColorText());
                obj_out.writeObject ( ((DefaultTableModel)(useCaseTable.getModel())).getDataVector() );
                System.out.println( ((DefaultTableModel)(useCaseTable.getModel())).getDataVector().toString());
              }
            catch (Exception e){//Catch exception if any
                System.err.println("Error: " + e.getMessage());
            }
        }
    }

    public void openDiagramPressed(DrawPanel dPane, UseCaseGUI gui){
        JFrame frame = new JFrame();
        JFileChooser chooser = new JFileChooser();
        int returnVal = chooser.showOpenDialog(frame);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File newFile = chooser.getSelectedFile();
            try{
                FileInputStream fstream = new FileInputStream(newFile);
                ObjectInputStream obj_in = new ObjectInputStream (fstream);
                // Read an object
                UseCaseController.myUCModel.setUseCaseVector((Vector) obj_in.readObject());
                gui.setColorText((Document) obj_in.readObject());

                Vector v1 = (Vector) obj_in.readObject();
                Vector v2 = new Vector();
                v2.add("Actor");
                v2.add("Verb Noun Phrase");
                v2.add("System");
                v2.add("Verified");
                UseCaseTableModel model = new UseCaseTableModel(v1, v2, gui);
                gui.setUseCaseCount(model.getRowCount()-1);
                gui.setUCTableModel(model);
            }
            catch (Exception e){//Catch exception if any
                System.err.println("Error: " + e.getMessage());
            }
        }
    }

}
